package com.minimalist.lindi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.minimalist.common.config.exception.BusinessException;
import com.minimalist.common.config.mybatis.bo.PageResp;
import com.minimalist.common.entity.basic.enums.FileEnum;
import com.minimalist.common.entity.basic.vo.file.FileVO;
import com.minimalist.common.entity.lindi.enums.LindiEnterpriseAccountEnum;
import com.minimalist.common.entity.lindi.enums.LindiEnterpriseRegistrationEnum;
import com.minimalist.common.entity.lindi.po.LindiEnterpriseRegistration;
import com.minimalist.common.entity.lindi.vo.enterpriseregistration.LindiEnterpriseRegistrationAuditVO;
import com.minimalist.common.entity.lindi.vo.enterpriseregistration.LindiEnterpriseRegistrationQueryVO;
import com.minimalist.common.entity.lindi.vo.enterpriseregistration.LindiEnterpriseRegistrationVO;
import com.minimalist.common.feign.client.basic.FileFeignClient;
import com.minimalist.common.utils.UnqIdUtil;
import com.minimalist.lindi.manager.LindiEnterpriseManager;
import com.minimalist.lindi.mapper.LindiEnterpriseRegistrationMapper;
import com.minimalist.lindi.service.LindiEnterpriseRegistrationService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 林地指标交易平台_企业注册表 服务层实现。
 *
 * @author 11987
 * @since 2025-03-31
 */
@Service
public class LindiEnterpriseRegistrationServiceImpl
        extends ServiceImpl<LindiEnterpriseRegistrationMapper, LindiEnterpriseRegistration>
        implements LindiEnterpriseRegistrationService{
    @Autowired
    private LindiEnterpriseRegistrationMapper mapper;

    @Autowired
    private LindiEnterpriseManager enterpriseManager;

    @Autowired
    private FileFeignClient fileFeignClient;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addEnterpriseRegistration(
            LindiEnterpriseRegistrationVO enterpriseRegistrationVO){
        Assert.isFalse(enterpriseManager.checkEnterpriseNameRegistered(
                        enterpriseRegistrationVO.getEnterpriseName(), null),
                () -> new BusinessException(
                        LindiEnterpriseRegistrationEnum.ErrorMsgEnum.ENTERPRISE_NAME_REGISTERED
                                .getDesc()));
        Assert.isFalse(enterpriseManager.checkEnterpriseMobileRegistered(
                        enterpriseRegistrationVO.getEnterpriseMobile(), null),
                () -> new BusinessException(
                        LindiEnterpriseRegistrationEnum.ErrorMsgEnum.ENTERPRISE_MOBILE_REGISTERED
                                .getDesc()));
        LindiEnterpriseRegistration enterpriseRegistration = new LindiEnterpriseRegistration();
        BeanUtil.copyProperties(enterpriseRegistrationVO, enterpriseRegistration);
        enterpriseRegistration.setEnterpriseRegistrationId(UnqIdUtil.uniqueId());
        enterpriseRegistration.setStatus(LindiEnterpriseRegistrationEnum.StatusEnum.PENDING.getCode());
        mapper.insert(enterpriseRegistration);
    }

    @Override
    public void deleteEnterpriseRegistrationByEnterpriseRegistrationId(
            Long enterpriseRegistrationId){
        LindiEnterpriseRegistration enterpriseRegistration = mapper
                .selectEnterpriseRegistrationByEnterpriseRegistrationId(enterpriseRegistrationId);
        Assert.notNull(enterpriseRegistration, () -> new BusinessException(
                LindiEnterpriseRegistrationEnum.ErrorMsgEnum.ENTERPRISE_REGISTRATION_NOT_EXIST
                        .getDesc()));
        mapper.deleteEnterpriseRegistrationByEnterpriseRegistrationId(
                enterpriseRegistrationId);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateEnterpriseRegistrationByEnterpriseRegistrationId(
            LindiEnterpriseRegistrationVO enterpriseRegistrationVO){
        Assert.isFalse(enterpriseManager.checkEnterpriseNameRegistered(
                        enterpriseRegistrationVO.getEnterpriseName(), null),
                () -> new BusinessException(
                        LindiEnterpriseAccountEnum.ErrorMsgEnum.ENTERPRISE_NAME_REGISTERED
                                .getDesc()));
        LindiEnterpriseRegistration lindiEnterpriseRegistration = new LindiEnterpriseRegistration();
        BeanUtil.copyProperties(enterpriseRegistrationVO, lindiEnterpriseRegistration);
        mapper.updateEnterpriseRegistrationByEnterpriseRegistrationId(
                lindiEnterpriseRegistration);
    }

    @Override
    public PageResp<LindiEnterpriseRegistrationVO> getPageEnterpriseRegistrationList(
            LindiEnterpriseRegistrationQueryVO queryVO){
        Page<LindiEnterpriseRegistration> page = mapper
                .selectPageEnterpriseRegistrationList(queryVO);
        List<LindiEnterpriseRegistrationVO> list = BeanUtil.copyToList(
                page.getRecords(), LindiEnterpriseRegistrationVO.class);
        return new PageResp<>(list, page.getTotalRow());
    }

    @Override
    public LindiEnterpriseRegistrationVO getEnterpriseRegistrationByEnterpriseRegistrationId(
            Long enterpriseRegistrationId){
        // 获取企业注册基本信息
        LindiEnterpriseRegistration registration = mapper
                .selectEnterpriseRegistrationByEnterpriseRegistrationId(enterpriseRegistrationId);
        Assert.notNull(registration, () -> new BusinessException(
                LindiEnterpriseRegistrationEnum.ErrorMsgEnum.ENTERPRISE_REGISTRATION_NOT_EXIST
                        .getDesc()));

        LindiEnterpriseRegistrationVO registrationVO = BeanUtil.copyProperties(
                registration, LindiEnterpriseRegistrationVO.class);
        // 收集所有需要查询的文件ID
        List<Long> fileIds = new java.util.ArrayList<>();
        if (registrationVO.getEnterpriseBusinessLicense() != null){
            fileIds.add(registrationVO.getEnterpriseBusinessLicense());
        }
        if (registrationVO.getEnterpriseTax() != null){
            fileIds.add(registrationVO.getEnterpriseTax());
        }
        if (registrationVO.getEnterpriseCode() != null){
            fileIds.add(registrationVO.getEnterpriseCode());
        }
        if (registrationVO.getEnterpriseIdcard1() != null){
            fileIds.add(registrationVO.getEnterpriseIdcard1());
        }
        if (registrationVO.getEnterpriseIdcard2() != null){
            fileIds.add(registrationVO.getEnterpriseIdcard2());
        }
        if (registrationVO.getEnterpriseAttorney() != null){
            fileIds.add(registrationVO.getEnterpriseAttorney());
        }

        // 如果有文件ID需要查询
        if (!fileIds.isEmpty()){
            List<FileVO> fileVOList = fileFeignClient.getFileListByFileIds(fileIds)
                    .getData();
            if (fileVOList == null || fileVOList.isEmpty()){
                throw new BusinessException(FileEnum.ErrorMsg.NONENTITY_FILE.getDesc());
            }
            // 批量查询文件信息
            java.util.Map<Long, String> fileUrlMap = fileVOList
                    .stream()
                    .collect(java.util.stream.Collectors.toMap(
                            FileVO::getFileId,
                            FileVO::getFileUrl));
            // 设置文件URL
            if (registrationVO.getEnterpriseBusinessLicense() != null){
                registrationVO.setEnterpriseBusinessLicenseUrl(
                        fileUrlMap.get(registrationVO.getEnterpriseBusinessLicense()));
            }
            if (registrationVO.getEnterpriseTax() != null){
                registrationVO.setEnterpriseTaxUrl(
                        fileUrlMap.get(registrationVO.getEnterpriseTax()));
            }
            if (registrationVO.getEnterpriseCode() != null){
                registrationVO.setEnterpriseCodeUrl(
                        fileUrlMap.get(registrationVO.getEnterpriseCode()));
            }
            if (registrationVO.getEnterpriseIdcard1() != null){
                registrationVO.setEnterpriseIdcard1Url(
                        fileUrlMap.get(registrationVO.getEnterpriseIdcard1()));
            }
            if (registrationVO.getEnterpriseIdcard2() != null){
                registrationVO.setEnterpriseIdcard2Url(
                        fileUrlMap.get(registrationVO.getEnterpriseIdcard2()));
            }
            if (registrationVO.getEnterpriseAttorney() != null){
                registrationVO.setEnterpriseAttorneyUrl(
                        fileUrlMap.get(registrationVO.getEnterpriseAttorney()));
            }
        }

        return registrationVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void auditEnterpriseRegistration(LindiEnterpriseRegistrationAuditVO auditVO){
        LindiEnterpriseRegistration lindiEnterpriseRegistration = mapper
                .selectEnterpriseRegistrationByEnterpriseRegistrationId(
                        auditVO.getEnterpriseRegistrationId());
        Assert.notNull(lindiEnterpriseRegistration, () -> new BusinessException(
                LindiEnterpriseRegistrationEnum.ErrorMsgEnum.ENTERPRISE_REGISTRATION_NOT_EXIST
                        .getDesc()));
        lindiEnterpriseRegistration.setStatus(auditVO.getStatus());
        mapper.updateEnterpriseRegistrationByEnterpriseRegistrationId(
                lindiEnterpriseRegistration);

        // 审核通过，创建企业账户
        if (auditVO.getStatus().equals(LindiEnterpriseRegistrationEnum
                .StatusEnum.APPROVED.getCode())){
            enterpriseManager.createEnterpriseAccount(lindiEnterpriseRegistration);
        }
    }
}
